package com.allinpay;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.UnauthorizedException;
import org.junit.Test;

import java.util.Arrays;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 14-1-26
 * <p>Version: 1.0
 */
public class RoleTest extends BaseTest {
    private final static Log log = LogFactory.getLog(RoleTest.class);
    @Test
    public void testHasRole() {
        super.login("classpath:shiro-role.ini", "admin", "123");
        //判断拥有角色：role1
        log.info(getSubject().hasRole("role1"));
        //判断拥有角色：role1 and role2
        log.info(getSubject().hasAllRoles(Arrays.asList("role1", "role2")));
        //判断拥有角色：role1 and role2 and !role3
        boolean[] result = getSubject().hasRoles(Arrays.asList("role1", "role2", "role3"));
        log.info(result[0]);
        log.info(result[1]);
        log.info(result[2]);
    }

    @Test(expected = UnauthorizedException.class)
    public void testCheckRole() {
        super.login("classpath:shiro-role.ini", "admin", "123");
        //断言拥有角色：role1
        super.getSubject().checkRole("role1");
        //断言拥有角色：role1 and role3 失败抛出异常
        super.getSubject().checkRoles("role1", "role3");
    }

}
